var actmodel = function () {



  this.post = function (urlpara, data) {
    var app = getApp();

    if (!urlpara["do"] && actmodel.do) {
      urlpara["do"] = actmodel.do
    }
    console.log("oooooooooooo")
    console.log(urlpara)
    console.log(data)
   
    var url = app.util.url('entry/wxapp', urlpara)
    var that = this;
    return new Promise((resolve, reject) => {
      app.util.request({
        url,
        'data': data,
        'method': "post",
        showLoading: false,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.data.errno == 0) {
            resolve(res.data.data);
          }
          reject(res.data.message);
        },
        fail: function (res) {
          reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
        }
      });
    });
  }
  this.wherepara = {};
  this.syspage = {
    loadstatus: "loadmore",
    vdata: 0, //上次加载时间
    offset: 0,
    limit: 15,
    xcsorts: [
      ["sorts", "desc"],
      ["id", "desc"]
    ]
  }
  // 1重置， 2.重新查询上次查询【一些是加载数据失败重新加载】
  this.resetpage = function (reset) {
    var syspage = this.syspage;
    if (reset == -1) {} else if (reset == 1) {
      syspage.offset = 0;
      syspage.limit = 15;
      syspage.loadstatus = "loadmore";
      syspage.vdata = (new Date()).valueOf();
    } else if (reset == 2) {
      syspage.loadstatus = "loadmore";
      syspage.vdata = (new Date()).valueOf();
    }
  }
  this.getwhere = function (op) {
    var strwhere = {};
    if (op) {
      strwhere["op"] = op;
    }
    strwhere["offset"] = this.syspage.offset;
    strwhere["limit"] = this.syspage.limit;
    strwhere["xcsorts"] = JSON.stringify(this.syspage.xcsorts);
    if (this.wherepara) {
      for (var a in this.wherepara) {
        if (typeof (this.wherepara[a]) == "object") {
          strwhere[a] = JSON.stringify(this.wherepara[a])
        } else {
          strwhere[a] = this.wherepara[a];
        }
      }
    }
    return strwhere;
  }
  this.setstatus = function (that, loadstatus) {
    var syspage = this.syspage;
    var xcloadCom = that.selectComponent("#xcload");



    if (xcloadCom) {
      xcloadCom.updatestatus(loadstatus);
    }
    syspage.loadstatus = loadstatus;
  }
  /* 分页加载
reset -1 上次查询条件，  1重置， 2.重新查询上次查询【一些是加载数据失败重新加载】 xx
*/
  this.pagelist = function (acturlpara, that, reset, para, xcsorts, nodataend) {
    if (xcsorts) {
      this.syspage.xcsorts = xcsorts;
    }
    var app = getApp();
    var syspage = this.syspage;
    this.resetpage(reset);
    if (para) {
      this.wherepara = para;
    }
    if (syspage.loadstatus == "loading") {
      return new Promise((resolve, reject) => {
        resolve({
          status: -2,
          code: "loading"
        });
      })
    } else if (syspage.loadstatus == "over") {
      return new Promise((resolve, reject) => {
        resolve({
          status: -2,
          code: "over"
        });
      })
    }
    //置为加载中
    this.setstatus(that, "loading");
    var actthis = this;
    return new Promise((resolve, reject) => {
      var temtimestamp = (new Date()).valueOf()
      app.util.request({
        'url': acturlpara.url,
        showLoading: false,
        'data': actthis.getwhere(this.wherepara["op"] ? this.wherepara["op"] : "offset"),
        'method': acturlpara.method ? acturlpara.method : "post",
        success: function (res) {


          if (res.data.data["status"] != -1) {
            if (temtimestamp >= syspage.vdata) {
              var resultmodel = res.data.data
              var ilen = resultmodel["obj"]["modellist"].length;
              if (nodataend) {
                ilen = resultmodel["obj"]["len"]
              }
              var code = ""
              if (ilen < syspage.limit) {
                code = "over";
                syspage.offset = syspage.offset + ilen;
              } else {
                code = "loadmore";
                syspage.offset = syspage.offset + syspage.limit;
              }
              resultmodel["code"] = code;
              actthis.setstatus(that, code)
              resolve(resultmodel);
            } else {
              var resultmodel = {};
              //被抛弃数据
              resultmodel["code"] = "warin";
              resultmodel["status"] = -2;
              resolve(resultmodel);
            }
          } else {
            var resultmodel = {};
            resultmodel["code"] = "erro"
            syspage.loadstatus = "erro";

            actthis.setstatus(that, "erro")
            resolve(resultmodel);
          }
        },
        fail: function (res) {
          var resultmodel = {};
          resultmodel["code"] = "erro"
          actthis.setstatus(that, "erro")
          reject(res);
        }

      });
    });
  }
}
module.exports = actmodel;